home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / STDLIB.PAK / LIST.CPP < prev    next >
Text File  |  1997-05-06  |  1KB  |  63 lines

  1.  #include <list>
  2.  #include <string>
  3.  
  4.  using namespace std;
  5.  
  6.  //
  7.  // Print out a list of strings.
  8.  //
  9.  ostream& operator<< (ostream& out, const list<string>& l)
  10.  {
  11.    copy(l.begin(), l.end(), ostream_iterator<string>(cout," "));
  12.    return out;
  13.  }
  14.  
  15.  int main ()
  16.  {
  17.    //
  18.    // Create a list of critters.
  19.    //
  20.    list<string> critters;
  21.    int i;
  22.    //
  23.    // Insert several critters.
  24.    //
  25.    critters.insert(critters.begin(),"antelope");
  26.    critters.insert(critters.begin(),"bear");
  27.    critters.insert(critters.begin(),"cat");
  28.    //
  29.    // Print out the list.
  30.    //
  31.    cout << critters << endl;
  32.    //
  33.    // Change cat to cougar.
  34.    //
  35.    *find(critters.begin(),critters.end(),"cat") = "cougar";
  36.    cout << critters << endl;
  37.    //
  38.    // Put a zebra at the beginning, an ocelot ahead of antelope,
  39.    // and a rat at the end.
  40.    //
  41.    critters.push_front("zebra");
  42.    critters.insert(find(critters.begin(),critters.end(),"antelope"),"ocelot");
  43.    critters.push_back("rat");
  44.    cout << critters << endl;
  45.    //
  46.    // Sort the list (Use list's sort function since the
  47.    // generic algorithm requires a random access iterator
  48.    // and list only provides bidirectional)
  49.    //
  50.    critters.sort();
  51.    cout << critters << endl;
  52.    //
  53.    // Now let's erase half of the critters.
  54.    //
  55.    int half = critters.size() / 2;
  56.    for (i = 0; i < half; ++i)
  57.      critters.erase(critters.begin());
  58.    cout << critters << endl;
  59.  
  60.    return 0;
  61.  }
  62.  
  63.